from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import TemplateDoesNotExist
from django.views.generic.simple import direct_to_template #about_pages view
from django.shortcuts import render_to_response
from tb.models import *
from django import forms
from django.db.models import Q
def showpath(request, pname):
  import pydot
  metlink_list=Tbmet_link.objects.filter(pathway__name=pname)
  G=pydot.Dot()
  el=[]
  for link in metlink_list:
              e=pydot.Edge(link.com1.compoundid,link.com2.compoundid, label=link.rea)
              G.add_edge(e)
 #    el.append((link.com1,link.com2,link.rea))
#  G=pydot.graph_from_edges(el)
  G.write_jpg('C:/human/media/'+pname+'.jpg', prog='dot')
  image=open('C:/human/media/'+pname+".jpg","rb").read()
  return HttpResponse(image, mimetype="image/jpg")

def showpathsvg(request, pname):
  import pydot
  metlink_list=Tbmet_link.objects.filter(pathway__name=pname)
  G=pydot.Dot()
  el=[]
  for link in metlink_list:
              e=pydot.Edge(link.com1.name,link.com2.name, label=link.rea)
              G.add_edge(e)
              n=pydot.Node(link.com1.name,tooltip=link.com1.compoundid)
              G.add_node(n)
              n=pydot.Node(link.com2.name,tooltip=link.com2.compoundid)
              G.add_node(n)
 #    el.append((link.com1,link.com2,link.rea))
#  G=pydot.graph_from_edges(el)
  G.write_svg('C:/human/media/'+pname+'.svg', prog='dot')
  image=open('C:/human/media/'+pname+".svg","rb").read()
  return HttpResponse(image, mimetype="image/svg+xml")

def showpaths(request):
   path_list=Tbpathway.objects.all()
   return render_to_response("tbpath_list.html", locals())
